package hardware;

import java.util.LinkedList;

public class Commit {
	static LinkedList<Instruction> instructions= new LinkedList<Instruction>();
	
	/**
	 * commits the instructions ready to be committed. 
	 * @param rob
	 *           list of rob entries that i have.
	 * @param currentTime
	 *                  the current time at which the operation is performed.
	 */
 public static void commit(LinkedList<ROBEntry> rob, long currentTime){
	 
	 if(instructions.size() !=0){
	 while(!rob.isEmpty()){
		  
          if(rob.getFirst().commit()){
			 int index = getInstById(rob.removeFirst().getID());
			 instructions.get(index).setCommitEndTime(currentTime);
			 instructions.get(index).setCommited(true);
			 instructions.get(index).getRegA().setReady(true);
			 RegisterMap.freeRenamed(instructions.get(index).getOriginalLogical());
			 break;
		 }else{
			 break;
		 }
	 }
	 }
 }
 
 /**
  * Gets the instruction from the list of instructions in the commit list with the given id.
  * @param id
  *          id of instruction i am searching for.
  * @return
  *        The instruction i am searching for.
  */
 public static int getInstById(int id){
	 for(int i=0;i<instructions.size();i++){
		 if(instructions.get(i).getID() == id && !instructions.get(i).isCommited()){
			 return i;
		 }
	 }
	 return -1;
 }
}
